﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 输入第一个数
            Console.WriteLine("请输入第一个数：");
            string num1 = Console.ReadLine();
            int number1 = CheckNum(num1);

            // 输入第二个数
            Console.WriteLine("请输入第二个数：");
            string num2 = Console.ReadLine();
            int number2 = CheckNum(num2);

            // 选择运算符
            Console.WriteLine("请选择运算符：1. +  2. -  3. x  4. ÷  5. %");
            string operation = Console.ReadLine();

            GetResult(operation, number1, number2);

            Console.ReadLine();
        }

        // 检测输入的字符串是否能转换为整数
        static int CheckNum(string num)
        {
            try
            {
                int parsedNumber = int.Parse(num);
                return parsedNumber;
            }
            catch (Exception e)
            {
                Console.WriteLine("输入有误，请重新输入：");
                string str = Console.ReadLine();
                return CheckNum(str);
            }
        }

        // 根据运算符计算结果并打印
        static void GetResult(string operation, int num1, int num2)
        {
            int result = 0;
            string operatorSymbol = "";

            switch (operation)
            {
                case "1":
                    result = num1 + num2;
                    operatorSymbol = "+";
                    break;
                case "2":
                    result = num1 - num2;
                    operatorSymbol = "-";
                    break;
                case "3":
                    result = num1 * num2;
                    operatorSymbol = "x";
                    break;
                case "4":
                    result = num1 / num2;
                    operatorSymbol = "÷";
                    break;
                case "5":
                    result = num1 % num2;
                    operatorSymbol = "%";
                    break;
                default:
                    Console.WriteLine("请重新选择运算符：");
                    string str = Console.ReadLine();
                    GetResult(str, num1, num2);
                    return;
            }

            Console.WriteLine($"{num1} {operatorSymbol} {num2} = {result}");
        }
    }
}
